package handler

import (
	"context"
	"go_final/mxshop_srvs/userop_srv/global"
	"go_final/mxshop_srvs/userop_srv/model"
	addresspb "go_final/mxshop_srvs/userop_srv/proto/protofile/address"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"google.golang.org/protobuf/types/known/emptypb"
)

type AddressServer struct{}

func (a AddressServer) GetAddressList(c context.Context, request *addresspb.AddressRequest) (*addresspb.AddressListResponse, error) {
	var addresses []model.Address
	res := global.DB.Where(model.Address{
		UserID: request.UserID,
	}).Find(&addresses)

	if res.Error != nil {
		return nil, res.Error
	}

	rsp := addresspb.AddressListResponse{
		Total: int32(res.RowsAffected),
	}

	for _, address := range addresses {
		rsp.Data = append(rsp.Data, &addresspb.AddressResponse{
			Id:           address.ID,
			UserID:       address.UserID,
			Province:     address.Province,
			City:         address.City,
			District:     address.District,
			Address:      address.Address,
			SignerName:   address.SignerName,
			SignerMobile: address.SignerMobile,
		})
	}

	return &rsp, nil
}

func (a AddressServer) CreateAddress(c context.Context, request *addresspb.AddressRequest) (*addresspb.AddressResponse, error) {
	// TODO 为了严谨性, 需要先调用用户服务查询userID是否存在
	address := model.Address{
		UserID:       request.UserID,
		Province:     request.Province,
		City:         request.City,
		District:     request.District,
		Address:      request.Address,
		SignerName:   request.SignerName,
		SignerMobile: request.SignerMobile,
	}

	res := global.DB.Save(&address)
	if res.Error != nil {
		return nil, res.Error
	}

	if res.RowsAffected == 0 {
		return nil, status.Error(codes.Internal, "新增地址失败")
	}

	return &addresspb.AddressResponse{
		Id: address.ID,
	}, nil
}

func (a AddressServer) DeleteAddress(c context.Context, request *addresspb.AddressRequest) (*emptypb.Empty, error) {
	res := global.DB.Model(&model.Address{
		UserID: request.UserID,
	}).Delete(&model.Address{}, request.Id)
	if res.Error != nil {
		return nil, res.Error
	}

	if res.RowsAffected == 0 {
		return nil, status.Error(codes.Internal, "删除地址失败")
	}

	return &emptypb.Empty{}, nil
}

func (a AddressServer) UpdateAddress(c context.Context, request *addresspb.AddressRequest) (*emptypb.Empty, error) {
	address := model.Address{}

	if request.Province != "" {
		address.Province = request.Province
	}
	if request.City != "" {
		address.City = request.City
	}
	if request.District != "" {
		address.District = request.District
	}
	if request.Address != "" {
		address.Address = request.Address
	}
	if request.SignerName != "" {
		address.SignerName = request.SignerName
	}
	if request.SignerMobile != "" {
		address.SignerMobile = request.SignerMobile
	}

	res := global.DB.Where(&model.Address{
		BaseModel: model.BaseModel{
			ID: request.Id,
		},
	}).Updates(&address)
	if res.Error != nil {
		return nil, res.Error
	}

	if res.RowsAffected == 0 {
		return nil, status.Error(codes.Internal, "更新地址失败")
	}

	return &emptypb.Empty{}, nil
}
